package com.xywy.tangandroid.activity;

import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.xywy.tangandroid.R;
import com.xywy.tangandroid.base.BaseActivity;
import com.xywy.tangandroid.bean.DoctorRepyQuestionDetail;
import com.xywy.tangandroid.bean.QuestionDetail;
import com.xywy.tangandroid.bean.User;
import com.xywy.tangandroid.util.Constant;
import com.xywy.tangandroid.util.DateUtil;
import com.xywy.tangandroid.util.ImageLoader;
import com.xywy.tangandroid.util.ImageLoader.ImageCallback;
import com.xywy.tangandroid.util.UIUtil;
import com.xywy.tangandroid.vollery.GetRequest;
import com.xywy.tangandroid.vollery.OnVolleyResponseListener;

/**
 * 在线提问问题，回答页面；
 */
public class QuestionDetailAct extends BaseActivity {
	protected static final int DOCTORDETAILERROR = -1;
	protected static final int DOCTORDETAILSUCESS = -2;
	private RelativeLayout left_img;
	private TextView middle_title;
	private RelativeLayout question_detail;
	private TextView question_title;
	private RelativeLayout display_rel;
	private TextView sex;
	private TextView age;
	private TextView data;
	private TextView question_content;
	private LinearLayout image_;
	private TextView doctor_name;
	private TextView doctor_academic;
	private TextView hosptail_name;
	private TextView recollections;
	private TextView doctor_time;
	private TextView question_type;
	private TextView doctot_suggest;
	private TextView textView4;
	private LinearLayout addImageLinear;
	private ImageLoader loader;
	private String id;
	private QuestionDetail detail;
	private DoctorRepyQuestionDetail doctorRepy;

	private LinearLayout.LayoutParams params;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_question_detail);
		getPreActicityData();
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText(getResources().getString(R.string.expert_replay));

		question_detail = (RelativeLayout) findViewById(R.id.question_detail);
		question_title = (TextView) findViewById(R.id.question_title);
		display_rel = (RelativeLayout) findViewById(R.id.display_rel);// 小图标
		sex = (TextView) findViewById(R.id.sex);

		age = (TextView) findViewById(R.id.age);
		data = (TextView) findViewById(R.id.data);
		question_content = (TextView) findViewById(R.id.question_content);
		image_ = (LinearLayout) findViewById(R.id.image_);
		doctor_name = (TextView) findViewById(R.id.doctor_name);

		doctor_academic = (TextView) findViewById(R.id.doctor_academic);
		hosptail_name = (TextView) findViewById(R.id.hosptail_name);
		recollections = (TextView) findViewById(R.id.recollectionss);
		doctor_time = (TextView) findViewById(R.id.doctor_time);
		question_type = (TextView) findViewById(R.id.question_type);
		doctot_suggest = (TextView) findViewById(R.id.doctot_suggest);
		textView4 = (TextView) findViewById(R.id.textView4);

		int ss = (int) getResources()
				.getDimension(R.dimen.person_center_height);
		addImageLinear = (LinearLayout) findViewById(R.id.image_line);
		params = new LinearLayout.LayoutParams(ss, ss);
		params.leftMargin = 80;
		params.gravity = Gravity.CENTER_VERTICAL;

		loader = new ImageLoader();
	}

	private String art_id; // 当前页面的文章id；

	/**
	 * 获取上个页面传递过来的数据
	 */
	private void getPreActicityData() {
		Intent intent = getIntent();
		art_id = intent.getStringExtra("art_id");
	}

	/**
	 * 加载是否提问的图片
	 * 
	 * @param imgPath
	 */
	void AddImageView(String imgPath) {
		ImageView imageView = new ImageView(context);
		// imageView.setImageBitmap(imgPath);

		Drawable cachedImage = loader.loadDrawable(imgPath, imageView,
				new ImageCallback() {

					@Override
					public void imageLoaded(Drawable imageDrawable,
							ImageView imageView, String imageUrl) {
						imageView = (ImageView) addImageLinear
								.findViewWithTag(imageUrl);
						if (imageView != null && imageDrawable != null) {
							imageView.setBackgroundDrawable(imageDrawable);
						}
					}
				});

		if (cachedImage != null) {
			imageView.setBackgroundDrawable(cachedImage);
		}
		imageView.setLayoutParams(params);
		addImageLinear.addView(imageView);
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
	}

	@Override
	protected void initData() {
		int flag = getIntent().getIntExtra("flag", -1);
		id = getIntent().getStringExtra("document_id");
		if (flag == -100) {
			display_rel.setVisibility(View.GONE);
		}
		getArticledetail();
	}

	/**
	 * 获取文章细节
	 */
	private void getArticledetail() {

		final Message msg = Message.obtain();
		GetRequest<String> getRequest = new GetRequest<String>(Constant.getUrl(
				"zhuanjia", "DoctorRelyDetails")
				+ "&id="
				+ id
				+ "&userid="
				+ User.getIUserInstance(context).getUserid() + "&source=cdsb",
				String.class, new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {

						msg.what = DOCTORDETAILERROR;
						handler.sendMessage(msg);
					}

					@Override
					public void onSuccess(String response) {
						JSONObject object;
						try {
							object = new JSONObject(response);
							int code = object.optInt("code");
							if (code == 10000) {

								PaseAretailJson(object);
								msg.what = DOCTORDETAILSUCESS;
								handler.sendMessage(msg);
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}

					}

				});
		getRequest.setIsParseJson(false);
		executeRequest(getRequest);

	}

	private void PaseAretailJson(JSONObject object) {
		String data = object.optString("data");
		Gson gson = new Gson();
		try {
			JSONObject object2 = new JSONObject(data);

			String zixun = object2.optString("zixun");
			detail = gson.fromJson(zixun, QuestionDetail.class);

			JSONObject reply = object2.getJSONObject("reply");
			Iterator<String> tempIterator = reply.keys();
			while (tempIterator.hasNext()) {
				JSONObject replyInner = reply
						.optJSONObject(tempIterator.next());
				doctorRepy = gson.fromJson(replyInner.toString(),
						DoctorRepyQuestionDetail.class);
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case DOCTORDETAILERROR:
				UIUtil.DissDialog(context, waittingDialog);
				UIUtil.showToast(context,
						getResources().getString(R.string.network_slow));
				break;
			case DOCTORDETAILSUCESS:
				UIUtil.DissDialog(context, waittingDialog);
				question_title.setText(detail.getTitle());
				sex.setText(detail.getZ_user());
				age.setVisibility(View.GONE);
				data.setText("发表于:" + DateUtil.getData(detail.getCreatime()));
				// tupian

				// http://static.i2.xywy.com/zhuanjia/20140919/04bb2f4be67e6329b496ffa06859ad5245320_d.jpg

				// 测试数据
				String imgurl = "http://static.i2.xywy.com/zhuanjia/"
						+ "20140919/04bb2f4be67e6329b496ffa06859ad5245320_d.jpg|"
						+ "http://static.i2.xywy.com/zhuanjia/20140919/04bb2f4be67e6329b496ffa06859ad5245320_d.jpg"
						+ "|http://static.i2.xywy.com/zhuanjia/20140919/04bb2f4be67e6329b496ffa06859ad5245320_d.jpg";
				// String imgurl=detail.getImage();
				String[] img = imgurl.split("\\|");
				for (int i = 0; i < img.length; i++) {
					if (img.length > 0)
						AddImageView(img[i]);
				}

				question_content.setText(detail.getIlldetail());
				// 图片展示的问题
				doctor_name.setText(doctorRepy.getName());
				// doctor_academic
				hosptail_name.setVisibility(View.GONE);
				recollections.setVisibility(View.GONE);

				doctor_academic.setText("发表于"
						+ DateUtil.getData(doctorRepy.getCreatime()));
				doctor_time.setVisibility(View.GONE);

				StringBuffer sb = new StringBuffer();
				sb.append(doctorRepy.getR_content());
				String[] content = doctorRepy.getR_content().split("\\|#\\|");

				System.out.println("内容....1" + doctorRepy.getR_content());

				question_type.setText(content[0]);
				if (content.length < 2) {
					doctot_suggest.setVisibility(View.GONE);
					textView4.setVisibility(View.GONE);
				} else
					doctot_suggest.setText(content[1]);
				break;

			default:
				break;
			}
		}
	};

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;

		default:
			break;
		}
	}

}
